package com.zpark.dao.impl;

import com.zpark.dao.IUserDao;
import com.zpark.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @PackageName: com.zpark.dao.impl
 * @ClassName: UserDaoImpl
 * @Description:
 * @author: RZS
 * @date: 2023/5/11  14:46
 */
@Repository
public class UserDaoImpl implements IUserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer insertUser(User user) {
        String sql = "insert into user(username, password, sex, language, city) values(?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getSex(),
                user.getLanguage(), user.getCity());
    }

    @Override
    public Integer deleteUserById(Integer id) {
        String sql = "delete from user where id=?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public Integer updateUserUnameById(String uname, Integer id) {
        String sql = "update user set username=? where id=?";
        return jdbcTemplate.update(sql, uname, id);
    }

    @Override
    public List<User> selectAll() {
        String sql = "select id, username, password, sex, language, city from user";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
    }

    @Override
    public User selectById(Integer id) {
        String sql = "select id, username, password, sex, language, city from user where id=?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
    }
}
